package your.rfgui.extgui.controller.table;

import org.apache.log4j.Logger;
import your.rfgui.extgui.data.intf.DataItem;
import your.rfgui.extgui.data.intf.ViewNode;

/**
 * A view node representing an item in the table GUI component
 */
public class TableViewNode implements ViewNode {

   private static final Logger log = Logger.getLogger(TableViewNode.class);
   /** indent space per level */
   protected static final StringBuilder indentPerLevel = new StringBuilder("   ");
   /** data of this table item */
   private DataItem dataItem;
   /** whether this item is a leaf node in a tree view representation */
   private boolean isLeaf = true;
   /** level in respect to the top level item in a tree view representation */
   private int level;

   /**
    * Create table view node positioned at top level and associated with the given data item
    */
   public TableViewNode(DataItem dataItem) {
      this.level = 0;
      this.dataItem = dataItem;
   }

   /**
    * Create table view node positioned under the given parent level and associated with the given data item
    */
   public TableViewNode(TableViewNode parent, DataItem dataItem) {
      this.level = parent.level + 1;
      parent.isLeaf = false;
      this.dataItem = dataItem;
   }

   /**
    * Get the data item of this view node
    */
   public DataItem getDataItem() {
      return dataItem;
   }

   /**
    * Get the label of the associated data item
    */
   public String getLabel() {
      return dataItem.getLabel();
   }

   /**
    * Get the type of the view node, simply the type of the associated data item
    */
   public String getType() {
      return dataItem.getClass().getSimpleName();
   }

   /**
    * String for debug purposes
    */
   @Override
   public String toString() {
      return getLabel();
   }

   /**
    * Whether this item is a leaf node in a tree view representation
    */
   public boolean isLeaf() {
      return isLeaf;
   }

   /**
    * String representing the indention of this item in a tree view representation
    */
   public String levelIndent() {
      StringBuilder indent = new StringBuilder(level * indentPerLevel.length());
      for (int i = 0; i < level; i++) {
         indent.append(indentPerLevel);
      }
      return indent.toString();
   }

   /**
    * Get level in respect to the top level item in a tree view representation
    */
   public int getLevel() {
      return level;
   }
}
